//
//  LYMineView.m
//  IPhoneX适配
//
//  Created by 训网高 on 2018/5/2.
//  Copyright © 2018年 训网高. All rights reserved.
//

#import "LYMineView.h"
#import "CYTMineTableViewCell.h"
//登录
#import "GXLoginViewController.h"
//设置
#import "LYSetViewController.h"
@interface LYMineView() <UITableViewDataSource,UITableViewDelegate>
{
    UIViewController *mineVC;
    
}
@end

@implementation LYMineView

- (instancetype)initWithFrame:(CGRect)frame withController:(FourthViewController *)viewController {
    self = [super initWithFrame:frame];
    if (self) {
        mineVC = viewController;
        [self creatTableView];
    }
    return self;
}

- (void)creatTableView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [[UIView alloc] init];
    
    [self addSubview:self.tableView];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 4;
    } else if (section == 1) {
        return 2;
    } else {
//        if ([GXUserModel shareInstance].isLogined) {
//            //如果是员工，返回三个
//            if([[NSString stringWithFormat:@"%@",[GXUserModel shareInstance].role_num] isEqualToString:@"2"]){
//                return 3;
//            } else {
//                return 2;
//            }
//        } else {
            return 2;
//        }
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
//        if ([GXUserModel shareInstance].isLogined == NO) {
            return 150;
//        } else {
//            return 225+15-50;
//        }
    } else {
        return CGFLOAT_MIN;
    }
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        UIView *headerView = [[UIView alloc] init];
        headerView.backgroundColor = RGB(244, 243, 245, 1.0);
//        if ([GXUserModel shareInstance].isLogined == NO) {
            UILabel * notloginLab = [[UILabel alloc]initWithFrame:CGRectMake(0, 11, SCREEN_WIDTH, 15)];
            notloginLab.text = @"登录游学宝";
            notloginLab.textAlignment = NSTextAlignmentCenter;
            //            notloginLab.textColor = titleViewColor;
            notloginLab.textColor = [UIColor blackColor];
            notloginLab.font = [UIFont systemFontOfSize:12];
            [headerView addSubview:notloginLab];
            
            UILabel * downLab = [[UILabel alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(notloginLab.frame)+10, SCREEN_WIDTH, 15)];
            downLab.text = @"实现您华丽转身的顾问、秘书、旅伴兼苦力";
            downLab.textAlignment = NSTextAlignmentCenter;
            //            downLab.textColor = titleViewColor;
            downLab.textColor = [UIColor blackColor];
            downLab.font = [UIFont systemFontOfSize:12];
            [headerView addSubview:downLab];
            
            UIButton * loginButton = [[UIButton alloc]initWithFrame:CGRectMake(10, CGRectGetMaxY(downLab.frame)+10, SCREEN_WIDTH - 20, 50)];
            //            [loginButton setBackgroundImage:[UIImage imageNamed:@"Login_button"] forState:UIControlStateNormal];
            loginButton.backgroundColor = RGB(83, 190, 218, 1);
            [loginButton setTitle:@"立即登录" forState:UIControlStateNormal];
            [loginButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            loginButton.titleLabel.font = [UIFont systemFontOfSize:19];
            loginButton.layer.cornerRadius = 5;
            loginButton.layer.masksToBounds = YES;
            [loginButton addTarget:self action:@selector(loginAction:) forControlEvents:UIControlEventTouchUpInside];
            [headerView addSubview:loginButton];
//        } else {
//            GXMineLoginView *mineLoginView = [GXMineLoginView createGxMineLoginView];
//            mineLoginView.frame = CGRectMake(0, 0, 0,0);
//
//            DLog(@"姓名：%@--职业：%@--个性说明：%@",[GXUserModel shareInstance].nikName,[GXUserModel shareInstance].job,[GXUserModel shareInstance].remark);
//            //姓名
//            if ([[GXUserModel shareInstance].nikName isNullOrEmpty]) {
//                mineLoginView.nameLabel.text = @"昵称";
//                mineLoginView.nameWidthConstraint.constant = 40;
//            }
//            else{
//                mineLoginView.nameLabel.text = [GXUserModel shareInstance].nikName;
//
//                NSDictionary *attributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:20.0]};
//
//                CGSize textSize = [mineLoginView.nameLabel.text boundingRectWithSize:CGSizeMake(200, 100) options:NSStringDrawingTruncatesLastVisibleLine attributes:attributes context:nil].size;
//                mineLoginView.nameWidthConstraint.constant = textSize.width+10;
//
//            }
//
//            //性别
//            if ([[GXUserModel shareInstance].sex isEqual:@"1"]) {
//                mineLoginView.genderImageView.image = [UIImage imageNamed:@"man_icon"];
//                mineLoginView.genderImageView.alpha = 1;
//            }
//            else if([[GXUserModel shareInstance].sex isEqual:@"2"])
//            {
//                mineLoginView.genderImageView.image = [UIImage imageNamed:@"girl_icon"];
//                mineLoginView.genderImageView.alpha = 1;
//            }
//            else{
//                mineLoginView.genderImageView.alpha = 0;
//            }
//
//            //头像
//            [mineLoginView.personImageView sd_setImageWithURL:[NSURL URLWithString:[GXUserModel shareInstance].headUrl] placeholderImage:[UIImage imageNamed:@"tx2"] options:SDWebImageAllowInvalidSSLCertificates];
//
//            //职业
//            if (![[GXUserModel shareInstance].job isNullOrEmpty]) {
//
//                mineLoginView.jobLabel.text = [NSString stringWithFormat:@"职业：%@",[GXUserModel shareInstance].job];
//            }
//            else
//            {
//                mineLoginView.jobLabel.text = @"职业";
//            }
//            //个性说明
//            if (![[GXUserModel shareInstance].remark isNullOrEmpty]) {
//
//                //                _explainLab.text = [GXUserModel shareInstance].remark;
//                mineLoginView.signLabel.text = [NSString stringWithFormat:@"个性说明：%@",[GXUserModel shareInstance].remark];
//            }
//            else
//            {
//                mineLoginView.signLabel.text = @"个性说明";
//            }
//
//            //我的二维码
//            [mineLoginView.QrCodeButton addTarget:self action:@selector(QrCodeButtonClick:) forControlEvents:UIControlEventTouchUpInside];
//
//            //个人主页
//            [mineLoginView.personalButton addTarget:self action:@selector(editPersonalDataAction) forControlEvents:UIControlEventTouchUpInside];
//            [headerView addSubview:mineLoginView];
//        }
        return headerView;
    } else {
        return nil;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (section == 2) {
        return 15;
    }else{
        return 15;
    }
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *cellIdentifier =   @"MineTableCell";
    
    CYTMineTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (cell == nil) {
        cell = [[CYTMineTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
    }
    
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            cell.headImage.image = [UIImage imageNamed:@"mine_collection"];
            
            cell.titleLabel.text = @"我的线路";
        } else if (indexPath.row == 1){
            cell.headImage.image = [UIImage imageNamed:@"mine_collection"];
            
            cell.titleLabel.text = @"我的结伴";
        } else if (indexPath.row == 2){
            cell.headImage.image = [UIImage imageNamed:@"mine_collection"];
            
            cell.titleLabel.text = @"我的出行";
        } else {
            cell.headImage.image = [UIImage imageNamed:@"mine_collection"];
            
            cell.titleLabel.text = @"我的好友";
        }
    } else if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            cell.headImage.image = [UIImage imageNamed:@"mine_recommend"];
            
            cell.titleLabel.text = @"推荐给好友";
        } else {
            cell.headImage.image = [UIImage imageNamed:@"mine_invite"];
            
            cell.titleLabel.text = @"填写邀请码";
        }
    } else {
//        if ([[NSString stringWithFormat:@"%@",[GXUserModel shareInstance].role_num] isEqualToString:@"2"]) {
//            if (indexPath.row == 0) {
//                cell.headImage.image = [UIImage imageNamed:@"mine_feedback"];
//
//                cell.titleLabel.text = @"意见反馈";
//            } else if (indexPath.row == 1){
//                cell.headImage.image = [UIImage imageNamed:@"mine_set"];
//
//                cell.titleLabel.text = @"设置";
//            } else {
//                cell.headImage.image = [UIImage imageNamed:@"mine_employees"];
//
//                cell.titleLabel.text = @"员工通道";
//            }
//
//        } else {
            if (indexPath.row == 0) {
                cell.headImage.image = [UIImage imageNamed:@"mine_feedback"];
                
                cell.titleLabel.text = @"意见反馈";
            } else {
                cell.headImage.image = [UIImage imageNamed:@"mine_set"];
                
                cell.titleLabel.text = @"设置";
            }
            
//        }
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        
    } else if (indexPath.section == 1) {
        
    } else {
        if (indexPath.row == 1) {
            //设置
            LYSetViewController *setVC = [[LYSetViewController alloc] init];
            mineVC.hidesBottomBarWhenPushed = YES;
            [mineVC.navigationController pushViewController:setVC animated:YES];
        }
    }
}

#pragma mark ---- 登录
- (void)loginAction:(UIButton *)sender {
    GXLoginViewController *loginVC = [[GXLoginViewController alloc] init];
    mineVC.hidesBottomBarWhenPushed = YES;
    [mineVC.navigationController pushViewController:loginVC animated:YES];
}

@end
